package qdu.com.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.hdfs.FileUtil;
import qdu.com.bean.WJsonBean1;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet("/rentc")
public class RentCarServlet extends HttpServlet {
    ObjectMapper mapper = new ObjectMapper();
    List<WJsonBean1> list = new ArrayList<WJsonBean1>();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<Integer, String[]> items = FileUtil.ReadPartFile(getServletContext().getRealPath("/output/output")+"/part-r-00001");
        for (Integer key:
                items.keySet()) {
            String[] value = items.get(key); // 4列数据
            list.add(new WJsonBean1(value[0],value[1],value[2],value[3]));
        }
        String json = mapper.writeValueAsString(list);
        resp.setHeader("content-type", "text/json;charset=utf-8");
        resp.getWriter().write(json);
    }

//         test
//    public static void main(String[] args) throws IOException {
//        ObjectMapper mapper = new ObjectMapper();
//        List<WJsonBean1> list = new ArrayList<WJsonBean1>();
//        Map<Integer, String[]> items = FileUtil.ReadPartFile("output/part-r-00001");
//        for (Integer key:
//                items.keySet()) {
//            String[] value = items.get(key); // 4列数据
//            list.add(new WJsonBean1(value[0],value[1],value[2],value[3]));
//        }
////        for (WJsonBean1 j:
////             list) {
////            String e1=j.getTime();
////            String e2=j.getCasual();
////            String e3=j.getRegistered();
////            String e4=j.getCount();
////            System.out.println(e1+" "+e2+" "+e3+" "+e4);
////        }
//        System.out.println(list);
//
////        String json = mapper.writeValueAsString(list);
////        System.out.println(json);
//    }
}
